using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;

namespace CustomRoad
{
    public partial class PropertiesRoadUI : UserControl
    {
        #region Private Fields

        private bool duringSetControlValues = false;
        private RoadProperties roadProperties;

        #endregion Private Fields

        #region Public Constructors

        public PropertiesRoadUI()
        {
            InitializeComponent();
            this.roadProperties = new CustomRoad.RoadProperties(new List<RoadModifer>(), this.checkBox_PinToTerrain.Checked);

            // Disable the controls because they have no function yet.

            if (true)
            {
                this.checkBox_MirrorX.Visible = false;
                this.checkBox_CreateNewUVs.Visible = false;
                this.numericUpDown_UVScale.Visible = false;
                this.radioButton_Scale.Visible = false;
                this.numericUpDown_Scale.Visible = false;
            }
            GetControlValues(null, null);
        }

        #endregion Public Constructors

        #region Public Delegates

        public delegate void PropertiesChanged();

        #endregion Public Delegates

        #region Public Events

        public event PropertiesChanged propertiesChanged;

        #endregion Public Events

        #region Public Properties

        public RoadProperties RoadProperties
        {
            get
            {
                return new RoadProperties(this.roadProperties);
                /*  = new RoadProperties();
                 roadProperties.Properties = new List<RoadModifer>(this.roadProperties.Properties.ToArray());
                 roadProperties.RoadType = this.roadProperties.RoadType;
                 roadProperties.PinToTerrain = this.checkBox_PinToTerrain.Checked;
                 roadProperties.Wr2Traffic = this.roadProperties.Wr2Traffic;
                 roadProperties.InclinationStart = this.roadProperties.
                 */
                return roadProperties;
            }
            set
            {
                this.roadProperties = new CustomRoad.RoadProperties(value);
                /*
                this.roadProperties = new CustomRoad.RoadProperties(value.Properties, value.PinToTerrain);
                this.roadProperties.RoadType = value.RoadType;
                this.roadProperties.Wr2Traffic = value.Wr2Traffic;
                 */

                this.listBox_CurrentObjects.Items.Clear();
                for (int i = 0; i < this.roadProperties.Properties.Count; i++)
                {
                    this.listBox_CurrentObjects.Items.Add(this.roadProperties.Properties[i]);
                }
                SetControlValues(null, null);
                if (this.propertiesChanged != null)
                    this.propertiesChanged();
            }
        }

        #endregion Public Properties

        #region Private Methods

        private void AddItemToList(object sender, EventArgs e)
        {
            if (File.Exists(this.fileTreeView1.SelectedFullPath))
            {
                this.roadProperties.Properties.Add(new RoadModifer(this.fileTreeView1.SelectedPath, 1.0f, false,
                    false, (float)this.numericUpDown_UVScale.Value, ScaleMode.AutoScale));

                this.listBox_CurrentObjects.Items.Add(this.roadProperties.Properties[this.roadProperties.Properties.Count - 1]);

                if (this.propertiesChanged != null)
                    propertiesChanged();
            }
        }

        private void button_SelectedItemDown_Click(object sender, EventArgs e)
        {
            for (int i = this.listBox_CurrentObjects.SelectedIndices.Count - 1; i >= 0; i--)
            {
                int index = this.listBox_CurrentObjects.SelectedIndices[i];
                if (index < this.listBox_CurrentObjects.Items.Count - 1 && !this.listBox_CurrentObjects.SelectedIndices.Contains(index + 1))
                {
                    this.listBox_CurrentObjects.Items.Insert(index + 2, this.listBox_CurrentObjects.Items[index]);
                    this.listBox_CurrentObjects.Items.RemoveAt(index);
                    this.listBox_CurrentObjects.SelectedIndices.Add(index + 1);

                    this.roadProperties.Properties.Insert(index + 2, this.roadProperties.Properties[index]);
                    this.roadProperties.Properties.RemoveAt(index);
                }

                if (this.propertiesChanged != null)
                    propertiesChanged();
            }
        }

        private void button_SelectedItemRemove_Click(object sender, EventArgs e)
        {
            int[] indices = new int[this.listBox_CurrentObjects.SelectedIndices.Count];
            this.listBox_CurrentObjects.SelectedIndices.CopyTo(indices, 0);
            Array.Sort(indices);

            for (int i = indices.Length - 1; i >= 0; i--)
            {
                int index = this.listBox_CurrentObjects.SelectedIndices[i];

                this.listBox_CurrentObjects.Items.RemoveAt(index);
                this.roadProperties.Properties.RemoveAt(index);

                if (this.propertiesChanged != null)
                    propertiesChanged();
            }
        }

        private void button_SelectedItemUp_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < this.listBox_CurrentObjects.SelectedIndices.Count; i++)
            {
                int index = this.listBox_CurrentObjects.SelectedIndices[i];
                if (index > 0 && !this.listBox_CurrentObjects.SelectedIndices.Contains(index - 1))
                {
                    this.listBox_CurrentObjects.Items.Insert(index - 1, this.listBox_CurrentObjects.Items[index]);
                    this.listBox_CurrentObjects.Items.RemoveAt(index + 1);
                    this.listBox_CurrentObjects.SelectedIndices.Add(index - 1);

                    this.roadProperties.Properties.Insert(index - 1, this.roadProperties.Properties[index]);
                    this.roadProperties.Properties.RemoveAt(index + 1);
                }

                if (this.propertiesChanged != null)
                    propertiesChanged();
            }
        }

        private void GetControlValues(object sender, EventArgs e)
        {
            if (!this.duringSetControlValues)
            {
                if (this.radioButton_RoadTypeRepeat.Checked)
                    this.roadProperties.RoadType = streetTool.RoadType.Repeat;
                else if (this.radioButton_RoadTypeTunnelBridge.Checked)
                    this.roadProperties.RoadType = streetTool.RoadType.TunnelBridge;

                this.roadProperties.Wr2Traffic = this.trafficUIControl.GetTrafficData();
                this.roadProperties.InclinationStart = this.inclination1.InclinationStart;
                this.roadProperties.InclinationMiddle = this.inclination1.InclinationMiddle;
                this.roadProperties.InclinationEnd = this.inclination1.InclinationEnd;
                this.roadProperties.MaximumMiddleLineSegemet = (float)this.numericUpDown_MaximumMiddlelineSegment.Value;
                this.roadProperties.PinToTerrain = this.checkBox_PinToTerrain.Checked;

                for (int i = 0; i < this.listBox_CurrentObjects.SelectedIndices.Count; i++)
                {
                    int index = this.listBox_CurrentObjects.SelectedIndices[i];
                    RoadModifer properties = (RoadModifer)this.listBox_CurrentObjects.Items[index];

                    if (this.radioButton_AutoScale.Checked)
                        properties.ScaleMode = ScaleMode.AutoScale;
                    else if (this.radioButton_ScaleToNextPoint.Checked)
                        properties.ScaleMode = ScaleMode.ScaleToNextPoint;
                    else if (this.radioButton_Scale.Checked)
                        properties.ScaleMode = ScaleMode.ScaleValue;
                    else
                        throw new Exception("no Radio Button selected!");

                    properties.CreateNewUVs = this.checkBox_CreateNewUVs.Checked;
                    properties.MirrorX = this.checkBox_MirrorX.Checked;
                    properties.Scale = (float)this.numericUpDown_Scale.Value;

                    this.listBox_CurrentObjects.Items[index] = properties;
                    this.roadProperties.Properties[index] = properties;
                }
                if (this.propertiesChanged != null)
                    propertiesChanged();
            }
        }

        private void SetControlValues(object sender, EventArgs e)
        {
            duringSetControlValues = true;

            this.checkBox_PinToTerrain.Checked = this.roadProperties.PinToTerrain;
            switch (this.roadProperties.RoadType)
            {
                case streetTool.RoadType.Repeat:
                    this.radioButton_RoadTypeRepeat.Checked = true;
                    this.radioButton_RoadTypeTunnelBridge.Checked = false;
                    break;

                case streetTool.RoadType.TunnelBridge:
                    this.radioButton_RoadTypeTunnelBridge.Checked = true;
                    this.radioButton_RoadTypeRepeat.Checked = false;
                    break;

                default:
                    throw new Exception(string.Format("RoadType unknown: {0}", this.roadProperties.RoadType));
            }

            this.trafficUIControl.SetTrafficData(this.roadProperties.Wr2Traffic);

            // inclination
            this.inclination1.InclinationStart = (int)this.roadProperties.InclinationStart;
            this.inclination1.InclinationMiddle = (int)this.roadProperties.InclinationMiddle;
            this.inclination1.InclinationEnd = (int)this.roadProperties.InclinationEnd;
            this.numericUpDown_MaximumMiddlelineSegment.Value = (int)this.roadProperties.MaximumMiddleLineSegemet;

            if (this.listBox_CurrentObjects.SelectedIndex > -1)
            {
                RoadModifer properties = (RoadModifer)this.listBox_CurrentObjects.SelectedItem;
                this.radioButton_AutoScale.Checked = (properties.ScaleMode == ScaleMode.AutoScale) ? true : false;
                this.radioButton_ScaleToNextPoint.Checked = (properties.ScaleMode == ScaleMode.ScaleToNextPoint) ? true : false;
                this.radioButton_Scale.Checked = (properties.ScaleMode == ScaleMode.ScaleValue) ? true : false;
                this.numericUpDown_Scale.Enabled = (this.radioButton_Scale.Checked) ? true : false;

                this.checkBox_MirrorX.Checked = properties.MirrorX;
                this.numericUpDown_Scale.Value = (decimal)properties.Scale;

                this.numericUpDown_UVScale.Enabled = this.checkBox_CreateNewUVs.Checked;

                if (this.propertiesChanged != null)
                    propertiesChanged();

                this.radioButton_AutoScale.Enabled = true;
                this.radioButton_Scale.Enabled = true;
                this.radioButton_ScaleToNextPoint.Enabled = true;
            }
            else
            {
                this.radioButton_AutoScale.Enabled = false;
                this.radioButton_Scale.Enabled = false;
                this.radioButton_ScaleToNextPoint.Enabled = false;
            }
            duringSetControlValues = false;
        }

        #endregion Private Methods
    }
}